var util = require('../../../libs/util.js');
var moment = require('../../../libs/moment.min.js');
moment.locale('zh-cn');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    count: 0,
    sql_delivery_date_start: moment().format('YYYY-MM-DD'),
    sql_delivery_date_end: moment().format('YYYY-MM-DD')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  selectDeliveryDateStart: function(e) {
    if(moment(this.data.sql_delivery_date_end).diff(moment(e.detail.value), 'months') > 1){
      wx.showModal({
        title: '温馨提示',
        content: '查询日期跨度不能超过一个月',
        success (res) {
          if (res.confirm) {
          }
        }
      })
      return false;
    }
    this.setData({
      sql_delivery_date_start: e.detail.value
    });
    //wx.setStorageSync('sql_delivery_date_start', e.detail.value);
    if (new Date(e.detail.value) > new Date(this.data.sql_delivery_date_end)) {
      this.setData({
        sql_delivery_date_end: util.dateObjectformat(new Date(e.detail.value), 'yyyy-MM-dd')
      })
      //wx.setStorageSync('sql_delivery_date_end', e.detail.value);
    }
    this.loadData();
  },
  selectDeliveryDateEnd: function(e) {
    if(moment(e.detail.value).diff(moment(this.data.sql_delivery_date_start), 'months') > 1){
      wx.showModal({
        title: '温馨提示',
        content: '查询日期跨度不能超过一个月',
        success (res) {
          if (res.confirm) {
          }
        }
      })
      return false;
    }
    this.setData({
      sql_delivery_date_end: e.detail.value
    });
    //wx.setStorageSync('sql_delivery_date_end', e.detail.value);
    if (new Date(e.detail.value) < new Date(this.data.sql_delivery_date_start)) {
      this.setData({
        sql_delivery_date_start: util.dateObjectformat(new Date(e.detail.value), 'yyyy-MM-dd')
      })
      //wx.setStorageSync('sql_delivery_date_start', e.detail.value);
    }
    this.loadData();
  },
  search: function () {
    this.loadData();
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    util.getData("/atapi/report/business/schedule_efficiency", {
      'CO_ID': app.globalData.CO_ID,
      'searchVal': that.data.searchVal,
      'page': that.data.page,
      'pageSize': that.data.pageSize,
      'mode':that.data.mode,
      'start_date':that.data.sql_delivery_date_start,
      'end_date':that.data.sql_delivery_date_end
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        var count = res.data.count;
        that.setData({
          dataList: res.data.data,
          count: count
        });
      } else {
        that.setData({
          dataList: [],
          user_id:app.globalData.user_id
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})